{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "65c9d03a",
   "metadata": {},
   "source": [
    "用def语句，依次写出函数名，括号，括号中的参数和冒号：，然后在缩进块中编写函数体，return语句返回"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "dc8ec0bc",
   "metadata": {},
   "outputs": [],
   "source": [
    "def my_abs(x):\n",
    "    if x >= 0:\n",
    "        return x\n",
    "    else:\n",
    "        return -x"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "ff5fea60",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "99\n"
     ]
    }
   ],
   "source": [
    "print(my_abs(-99))"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "46f2b166",
   "metadata": {},
   "source": [
    "如果没有return语句，函数执行完毕后也会返回结果，只是结果为None。return None可以简写为return"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "d63e96bf",
   "metadata": {},
   "source": [
    "### 空函数"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "54e74e07",
   "metadata": {},
   "source": [
    "如果想定义一个什么也不做的空函数，可以用pass语句"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "78dbc71b",
   "metadata": {},
   "outputs": [],
   "source": [
    "def nop():\n",
    "    pass"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "d2cf22d2",
   "metadata": {},
   "source": [
    "pass可以用来做占位符，先放一个pass，让代码运行起来，避免运行时出现语法错误"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "f277fc85",
   "metadata": {},
   "source": [
    "### 参数检查"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "66118d77",
   "metadata": {},
   "source": [
    "调用函数时，如果参数个数不对，Python解释器会自动检查出来"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "4de19c5a",
   "metadata": {},
   "outputs": [
    {
     "ename": "TypeError",
     "evalue": "test the error",
     "output_type": "error",
     "traceback": [
      "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m",
      "\u001b[1;31mTypeError\u001b[0m                                 Traceback (most recent call last)",
      "\u001b[1;32m~\\AppData\\Local\\Temp/ipykernel_21436/3228130215.py\u001b[0m in \u001b[0;36m<module>\u001b[1;34m\u001b[0m\n\u001b[0;32m      7\u001b[0m     \u001b[1;32melse\u001b[0m\u001b[1;33m:\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m      8\u001b[0m         \u001b[1;32mreturn\u001b[0m \u001b[1;33m-\u001b[0m\u001b[0mx\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m----> 9\u001b[1;33m \u001b[0mmy_abs\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;34m'A'\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m",
      "\u001b[1;32m~\\AppData\\Local\\Temp/ipykernel_21436/3228130215.py\u001b[0m in \u001b[0;36mmy_abs\u001b[1;34m(x)\u001b[0m\n\u001b[0;32m      2\u001b[0m \u001b[1;32mdef\u001b[0m \u001b[0mmy_abs\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mx\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m:\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m      3\u001b[0m     \u001b[1;32mif\u001b[0m \u001b[1;32mnot\u001b[0m \u001b[0misinstance\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mx\u001b[0m\u001b[1;33m,\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mint\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0mfloat\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m:\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m----> 4\u001b[1;33m         \u001b[1;32mraise\u001b[0m \u001b[0mTypeError\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;34m'test the error'\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m\u001b[0;32m      5\u001b[0m     \u001b[1;32mif\u001b[0m \u001b[0mx\u001b[0m \u001b[1;33m>=\u001b[0m \u001b[1;36m0\u001b[0m\u001b[1;33m:\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m      6\u001b[0m         \u001b[1;32mreturn\u001b[0m \u001b[0mx\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n",
      "\u001b[1;31mTypeError\u001b[0m: test the error"
     ]
    }
   ],
   "source": [
    "# isinstance()用于数据类型检查\n",
    "def my_abs(x):\n",
    "    if not isinstance(x,(int, float)):\n",
    "        raise TypeError('test the error')\n",
    "    if x >= 0:\n",
    "        return x\n",
    "    else:\n",
    "        return -x\n",
    "my_abs('A')"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "e9c9e046",
   "metadata": {},
   "source": [
    "### 返回多个值"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "4d65b00b",
   "metadata": {},
   "source": [
    "函数实际上返回的是一个tuple。在语法上返回一个tuple可以省略括号，多个变量可以同时接受一个tuple，按位置赋给对应的值。\n",
    "所以Python的函数返回多个值其实就是返回一个tuple。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "id": "a8a16776",
   "metadata": {},
   "outputs": [],
   "source": [
    "def test(x,y,z):\n",
    "    a = x + y + z\n",
    "    b = x - y -z\n",
    "    c = x * y * z\n",
    "    d = x / y / z\n",
    "    return a, b, c, d"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "id": "2dc465a6",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "(6, -4, 6, 0.16666666666666666)\n"
     ]
    }
   ],
   "source": [
    "a1 = test(1,2,3)\n",
    "print(a1)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "582edfbe",
   "metadata": {},
   "source": [
    "### 小结"
   ]
  },
  {
   "cell_type": "raw",
   "id": "53c0e151",
   "metadata": {},
   "source": [
    "1.定义函数时，需要确定函数名和参数个数\n",
    "2.如有必要，可以先对参数的数据类型做检查\n",
    "3.函数体内部可以用return随时返回函数结果\n",
    "4.函数执行完毕没有return的话，自动return None\n",
    "5.函数可以同时返回多个值，其实返回的是一个tuple"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "6b16b0af",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.9.7"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
